import java.io.*;
import java.net.*;
import java.nio.channels.*;

public class DownloadFile {
    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Usage: java DownloadFile <URL> <output-file>");
            return;
        }
        
        String urlStr = args[0];
        String outputFile = args[1];
        
        try {
            System.out.println("Downloading from: " + urlStr);
            System.out.println("Saving to: " + outputFile);
            
            URL url = new URL(urlStr);
            
            // Create parent directories if they don't exist
            File file = new File(outputFile);
            file.getParentFile().mkdirs();
            
            // Download using Java NIO
            try (ReadableByteChannel rbc = Channels.newChannel(url.openStream());
                 FileOutputStream fos = new FileOutputStream(outputFile)) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
            
            System.out.println("Download completed successfully!");
            
        } catch (Exception e) {
            System.out.println("Download failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
}